/**
 * Scene where enlarged photo is shown. Actual rendering is done by Grid object.
 * Handles gesture events and sets up the photo view accordingly.
 * @constructor
 */
function SceneDetails() {
    var SCREEN_W = 960;
    var rotationAngle = 0;
    var zoomDistance = 0;

    var isDescriptionOn = false,
    helpBarInfo1 = {
        helpbarType: "HELPBAR_TYPE_VOICE_CUSTOMIZE",
        helpbarItemsList: [
            { itemText: "Describe", commandList: [{command: "Describe"}]},
            { itemText: "Left", commandList: [{command: "Left"}]},
            { itemText: "Right", commandList: [{command: "Right"}]},
            { itemText: "Close", commandList: [{command: "Close"}]},
            { itemText: "Exit", commandList: [{command: "Exit"}]}
        ]
    },
    helpBarInfoDescribe1 = {
        helpbarType: "HELPBAR_TYPE_VOICE_SERVER_GUIDE_RETURN",
        guideText: "Say the word or phrase you wish to add as the image description"
    };
    
    this.initialize = function() {
        Grid.callbacks.onPhotoChanged = function() {
            rotationAngle = 0;
            zoomDistance = 0;
        };
        $('#PopupThoatDetails').sfPopup({
    		text:'Bạn có muốn thoát ứng dụng không?',
    		buttons:['Đồng ý', 'Hủy'],
    		defaultFocus: 1, 
    		callback:function(selectedIndex){
    			alert('selected button index: ' + selectedIndex);
    			if(selectedIndex==0){
    				sf.core.exit();
    			}
    		}
    	});
    };

    this.handleShow = function () {
        var helpBarInfo = {
            helpbarType: "HELPBAR_TYPE_GESTURE_CUSTOMIZE",
            itemList: [
                { itemType: "HELPBAR_GESTURE_ITEM_POSE_LIKE", itemText: "Like it" },
                { itemType: "HELPBAR_GESTURE_ITEM_ZOOM_OUT_IN", itemText: "Zoom in / out" },
                { itemType: "HELPBAR_GESTURE_ITEM_ROTATION", itemText: "Rotate photo" },
                { itemType: "HELPBAR_GESTURE_ITEM_RETURN", itemText: "Go back" }
            ]
        };

        webapis.recognition.SetGestureHelpbarInfo(JSON.stringify(helpBarInfo));
        
        webapis.recognition.SetVoiceHelpbarInfo(JSON.stringify(helpBarInfo1));

        $("#keyhelp").sfKeyHelp({
            "LEFTRIGHT" : "Di chuyển",
            "RETURN" : "Trở về"
        });
    };

    this.handleHide = function () {
        Grid.close();
    };

    this.handleFocus = function () {
        GestureDispatcher.currentScene = this;
        VoiceDispatcher.currentScene = this;
    };

    this.handleBlur = function () {
    };

    this.handleKeyDown = function(keyCode) {
        log("SceneDetails.handleKeyDown(" + keyCode + ")");

        switch (keyCode) {
        case sf.key.LEFT:
            Grid.left();
            break;

        case sf.key.RIGHT:
            Grid.right();
            break;

        case sf.key.RETURN:
            sf.scene.hide("Details");
            sf.scene.show("CookingGuide");
            sf.scene.focus("CookingGuide");
            sf.key.preventDefault();
            break;
        case sf.key.RED:
        	
    		Grid.helpmenu(0);
    	
        break;
        case sf.key.GREEN:
        	
    		Grid.helpmenu(1);
    	
        break;
        case sf.key.YELLOW:
        	
    		Grid.helpmenu(2);
    	
        break;
        case sf.key.BLUE:
        	
    		Grid.helpmenu(3);
    	
        break;
        case sf.key.EXIT:
        	$('#PopupThoatDetails').sfPopup('show');
        	break;
        }
    };

    this.handleGesture = function(e) {
        log("SceneDetails.handleGesture, type: " + e.eventtype);

        switch (e.eventtype) {
        case "EVENT_GESTURE_2HAND_ZOOM":
            // calculate zoomRatio ratio based on the accumulated distance

            zoomDistance += parseInt(e.result, 10);
            // clamp zoom distance value to some arbitrary range
            zoomDistance = Math.max(-SCREEN_W * 0.75, Math.min(SCREEN_W * 1.25, zoomDistance));
            // current zoom ratio
            var zoomRatio = (SCREEN_W + zoomDistance) / SCREEN_W;

            Grid.zoom(zoomRatio);
            break;

        case "EVENT_GESTURE_2HAND_ROTATE":
            // use accumulated rotation angle
            rotationAngle += parseInt(e.result, 10);
            Grid.rotation(rotationAngle);
            break;

        case "EVENT_GESTURE_LIKE":
            Grid.likePhoto();
            break;
        }
    };
    
    this.handleVoice = function(e) {
        log("+++++++++++++++++++++++++ >  SceneDetails.handleVoice, type: " + e.eventtype);

        switch (e.result.toLowerCase()) {
            case "left": Grid.left(); break;
            case "right": Grid.right(); break;
            case "exit": $('#PopupThoatDetails').sfPopup('show'); break;
            case "close":
                sf.scene.hide("Details");
                sf.scene.show("CookingGuide");
                sf.scene.focus("CookingGuide");
                sf.key.preventDefault();
                break;
            case "describe":
                webapis.recognition.SetVoiceHelpbarInfo(JSON.stringify(helpBarInfoDescribe1));
                isDescriptionOn = true;
                break;
            case "return":
                if (isDescriptionOn) {
                    webapis.recognition.SetVoiceHelpbarInfo(JSON.stringify(helpBarInfo1));
                    isDescriptionOn = false;
                }
                break;
            default:
                if (isDescriptionOn) {
                    Grid.setDescription(e.result);
                    webapis.recognition.SetVoiceHelpbarInfo(JSON.stringify(helpBarInfo1));
                    isDescriptionOn = false;
                }
                break;
        }
    };
}
